{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在实验二的基础上继续： <br>\n",
    "1、输入文件名在命令行输入（即输入文件可以指定）；输出文件名在命令行输出（即输出文件可以指定）； <br>\n",
    "2、可以输出指定省的信息 <br>\n",
    "具体示例： <br>\n",
    ">yq yq_in_03.txt yq_out_03.txt  <br>\n",
    "//表示输入文件名为yq_in_03.txt，输出文件名为yq_out_03.txt，包括所有省份信息，格式和实验二一致 <br>\n",
    ">yq yq_in_03.txt yq_out_03.txt 浙江省 <br>\n",
    "//输出文件中仅包括浙江省的信息 <br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " 123\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FileNotFoundError!请重新输入！\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " yq_in_03.txt\n"
     ]
    }
   ],
   "source": [
    "# 打开yq_in.txt文件\n",
    "while(1):\n",
    "    yq_in = input()\n",
    "    try:\n",
    "        f_in = open(yq_in, 'r', encoding = 'gb2312') \n",
    "        # 将文件内内容写入file\n",
    "        file = f_in.read()\n",
    "        f_in.close()\n",
    "        break\n",
    "    except FileNotFoundError:\n",
    "        print('FileNotFoundError!请重新输入！')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "list0=[]\n",
    "simple = []\n",
    "flag=0\n",
    "simple.append(file.split())  # file里的全部元素作为一个列表集合放入simple这个列表里\n",
    "for i in simple[0]:   # 遍历simple列表里第0个元素\n",
    "    list0.append(i)  # 将其添加到list0中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " 广东省\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广东省\n",
      "深圳\t417\n",
      "广州\t346\n",
      "东莞\t97\n",
      "佛山\t84\n",
      "珠海\t98\n",
      "江门\t23\n",
      "中山\t66\n",
      "惠州\t62\n",
      "湛江\t22\n",
      "茂名\t14\n",
      "肇庆\t19\n",
      "梅州\t16\n",
      "阳江\t14\n",
      "汕头\t25\n",
      "韶关\t10\n",
      "揭阳\t8\n",
      "汕尾\t5\n",
      "潮州\t5\n",
      "清远\t12\n",
      "河源\t4\n",
      "待明确地区\t0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "new_str = ''\n",
    "j = 0\n",
    "flag = 0\n",
    "limit = input()  # 由用户输入需要输出的省份名字\n",
    "for i in list0:\n",
    "    if(i == limit or list0[j-1] == limit or list0[j-2] == limit):\n",
    "        if(flag == 0):\n",
    "            new_str += i\n",
    "            new_str += '\\n' # 省份做单独一行\n",
    "            flag = 1\n",
    "\n",
    "        if(list0[j-1] == limit):  # 市区\n",
    "            new_str += i\n",
    "            new_str += '\\t'\n",
    "        elif(list0[j-2] == limit):  # 数量\n",
    "            new_str += i\n",
    "            new_str += '\\n'\n",
    "    j+=1\n",
    "print(new_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " yq_out_03.txt\n"
     ]
    }
   ],
   "source": [
    "yq_out = input()\n",
    "f_out=open(yq_out, \"w+\")\n",
    "f_out.write(new_str)\n",
    "f_out.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
